import flash.geom.Rectangle;
import flash.filters.BlurFilter;
import mx.transitions.easing.Regular;
import mx.utils.Delegate;

import kliment.ApplicationAbstract;
import kliment.display.MCC;
import kliment.geom.draw.Draw;
import kliment.layout.PopUpAbstract;
import kliment.transitions.Tween2;
import kliment.utils._Array;

/**
 * ...
 * @author	Klimet
 * @version	1.0.1
 */
class kliment.managers.PopUpManager {
	private static var _BLUR:Number = 2;
	private static var _instance:PopUpManager;
	private static var _popUpList:Array;
	private static var _app_mc:MovieClip;
	private static var _parent_mc:MovieClip;
	private static var _target_mc:MovieClip;
	private static var _cell_mc:MovieClip;
	private static var _hide_mc:MovieClip;
	private static var _blurFilter:BlurFilter;
	
	public function PopUpManager() {
		if (!_instance) {
			if (ApplicationAbstract.instance) {
				_parent_mc = ApplicationAbstract.instance.layout_popup_mc;
				_app_mc = ApplicationAbstract.instance.target_mc;
				_blurFilter = new BlurFilter(_BLUR, _BLUR, 1);
			} else 
				_parent_mc = MCC.create(_root);
			
			_target_mc = MCC.create(_parent_mc);
			_hide_mc = MCC.create(_target_mc);
			_cell_mc = MCC.create(_target_mc);
			
			Draw.rectangle(_hide_mc, new Rectangle(0, 0, Stage.width, Stage.height));
			_hide_mc._visible = false;
			_hide_mc._alpha = 0;
			Stage.addListener(this);
		}
	}
	
	static private function _init():Void {
		if (!_instance) {
			_instance = new PopUpManager();
		}
	}
	
	static public function addPopUp(window:PopUpAbstract, modal:Boolean):Void {
		_init();
		_popUpList.push(window);
		window.host = _instance;
		if (modal)
			lock();
		else
			unlock();
	}
	
	static public function centerPopUp(window:PopUpAbstract):Void {
		window.x = (Stage.width * .5 - window.width * .5) >> 0;
		window.y = (Stage.height * .5 - window.height * .5) >> 0;
	}
	
	static public function removePopUp(window:PopUpAbstract):Void {
		window.remove();
		_Array.remove(window, _popUpList);
		unlock();
	}
	
	static public function lock():Void {
		_hide_mc.onRelease = function():Void { }
		_hide_mc.useHandCursor = false;
		_hide_mc._visible = true;
		new Tween2(_hide_mc, '_alpha', Regular.easeOut, 30, .5, true);
		_app_mc.filters = [_blurFilter];
	}
	
	static public function unlock():Void {
		delete _hide_mc.onRelease;
		new Tween2(_hide_mc, '_alpha', Regular.easeOut, 0, .5, true);
		_app_mc.filters = [];
	}
	
	private function onResize():Void {
		_hide_mc._width = Stage.width;
		_hide_mc._height = Stage.height;
	}
	
	public function get content_mc():MovieClip { return _cell_mc }
}